package com.yanqu.road.dao.impl.activity.lanruosi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiUserData;
import com.yanqu.road.entity.activity.tripletown.Monster;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LanRuoSiUserDataDaoImpl extends TempDataObjectDao<LanRuoSiUserData> {


    public static String getLanRuoSiUserDataSql = "select * from `t_u_activity_lanruosi_user_data` where `activityId`=? and `userId`=?";
    public LanRuoSiUserData getUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getLanRuoSiUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertLanRuoSiUserDataSql = "insert into `t_u_activity_lanruosi_user_data`(activityId,userId,score,energy,lastRecTime,guideIndex,blockList1,blockList8,blockList16,waveNum,monsterList, " +
            " round) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LanRuoSiUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuideIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBlockList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBlockList8())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBlockList16())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWaveNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMonsterList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        if (getDb().executeNoneQuery(insertLanRuoSiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateLanRuoSiUserDataSql = "update `t_u_activity_lanruosi_user_data` set score=?,energy=?,lastRecTime=?,guideIndex=?,blockList1=?,blockList8=?,blockList16=?," +
            "waveNum=?,monsterList=?,round=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(LanRuoSiUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuideIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBlockList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBlockList8())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBlockList16())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWaveNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMonsterList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateLanRuoSiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected LanRuoSiUserData parseData(ResultSet rs) throws SQLException {
        LanRuoSiUserData data = new LanRuoSiUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setScore(rs.getLong("score"));
        data.setEnergy(rs.getInt("energy"));
        data.setLastRecTime(rs.getLong("lastRecTime"));
        data.setGuideIndex(rs.getInt("guideIndex"));
        data.setBlockList1(JSON.parseObject(rs.getString("blockList1"), int[][].class));
        data.setBlockList8(JSON.parseObject(rs.getString("blockList8"), int[][].class));
        data.setBlockList16(JSON.parseObject(rs.getString("blockList16"), int[][].class));
        data.setWaveNum(rs.getInt("waveNum"));
        data.setMonsterList(JSON.parseObject(rs.getString("monsterList"), new TypeReference<List<Monster>>() {}));
        data.setRound(rs.getInt("round"));
        return data;
    }

}
